package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"question/models"
	"strconv"
	"time"
)

type QuestionController struct {
	beego.Controller
}
type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *QuestionController) Add() {

	title := c.GetString("title")
	description := c.GetString("description")
	img := c.GetString("img")
	types := c.GetString("types")
	cate_id, _ := c.GetInt("cate_id")
	question := models.Question{

		Title:       title,
		Description: description,
		Img:         img,
		Types:       types,
		CateId:      uint(cate_id),
	}
	err := models.DB.Create(&question).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *QuestionController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 1,
			Msg:  "图片上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 1,
			Msg:  "图片过大",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 1,
			Msg:  "图片上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		imgSrc := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "图片上传成功",
			Data: imgSrc,
		}
		c.ServeJSON()

	}
}
func (c *QuestionController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	title := c.GetString("title")
	pageDate := make(map[string]interface{})
	query := models.DB.Model(&models.Question{})
	key := "page" + strconv.Itoa(page) + ":" + title
	val, err := models.RedisDB.Get(key).Bytes()
	if err != nil {

		var count int64
		query.Count(&count)
		offset := (page - 1) * size
		var questions []models.Question
		err = query.Preload("QuestionType").Offset(offset).Limit(size).Find(&questions).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			pageDate["count"] = count
			pageDate["quetions"] = questions
			jsonrByte, _ := json.Marshal(&pageDate)
			value := string(jsonrByte)
			models.RedisDB.Set(key, value, time.Second*15)

			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功-mysql",
				Data: pageDate,
			}
			c.ServeJSON()

		}

	} else {
		json.Unmarshal([]byte(val), &pageDate)

		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageDate,
		}
		c.ServeJSON()

	}

}
func (c *QuestionController) Info() {
	id, _ := c.GetInt("id")
	key := "id" + strconv.Itoa(id)
	val, err := models.RedisDB.Get(key).Result()
	var question models.Question
	if err != nil {
		err = models.DB.Where("id=?", id).First(&question).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "展示失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {

			jsonrByte, _ := json.Marshal(question)
			value := string(jsonrByte)
			models.RedisDB.Set(key, value, time.Second*15)
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "展示成功-mysql",
				Data: question,
			}
			c.ServeJSON()
			return
		}

	} else {
		json.Unmarshal([]byte(val), &question)
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "展示成功-redis",
			Data: question,
		}
		c.ServeJSON()
		return
	}
}
